package zkClient;

import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;

import java.util.List;

public class ZkClientMain {
    public static void main(String[] args) {
        ZkClient zkClient = new ZkClient("localhost:2181");
        zkClient.createPersistent("/t/t",true);
        //递归删除
        zkClient.deleteRecursive("/t/t");
        //获取子节点列表
        List<String> children = zkClient.getChildren("/t");
        //注册监听事件 可监听一个不存在的节点
        zkClient.subscribeChildChanges("/t-get", new IZkChildListener() {

            /**
             *
             * @param s path
             * @param list 变化后子节点列表
             * @throws Exception
             */
            @Override
            public void handleChildChange(String s, List<String> list) throws Exception {

            }
        });
        //读取数据
        boolean exists = zkClient.exists("/test");
        if(!exists){
            zkClient.createEphemeral("/test","asdfas");
            //读取数据
            Object o = zkClient.readData("/test");
            //注册监听
            zkClient.subscribeDataChanges("/test", new IZkDataListener() {
                @Override
                public void handleDataChange(String s, Object o) throws Exception {

                }

                @Override
                public void handleDataDeleted(String s) throws Exception {

                }
            });
        }

        //更新数据
        zkClient.writeData("/test","asdfsdf");
        //删除节点
        boolean delete = zkClient.delete("/test");


    }
}
